﻿using Unity.Burst;
using Unity.Entities;
using Unity.Collections;

[UpdateInGroup(typeof(LateSimulationSystemGroup))]
partial struct HealthDeadTestSystem : ISystem
{
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        //实体命令缓冲区
        // EntityCommandBuffer entityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);
        //通用实力命令缓冲区
        EntityCommandBuffer entityCommandBuffer = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);

        foreach ((RefRW<Health> health, Entity entity) in SystemAPI.Query<RefRW<Health>>().WithEntityAccess())
        {
            if (health.ValueRO.healthAmount <= 0)
            {
                //This Entitiy is dead
                //state.EntityManager.DestroyEntity(entity);
                //延迟 缓存加入
                UnityEngine.Debug.Log("Destory  :");
                health.ValueRW.onDead = true;
                entityCommandBuffer.DestroyEntity(entity);
            }
        }
        ////实体命令缓冲区执行缓存操作
        //entityCommandBuffer.Playback(state.EntityManager);
        //通用实力命令缓冲区 会在帧结束时播放，不用主动播放
    }
}
